import requests
import time
import random

# 初始数据
base_data = {
    "temp": [25.20, 26.13],
    "ph": [3.21, 6.72],
    "ntu": [530.25, 214.73],
    "ppm": [165.23, 57.15]
}

# 波动范围
fluctuation_range = 0.60

# 发送数据的 URL
url = "http://192.168.6.206:9912/data"

try:
    while True:
        # 生成波动后的数据
        fluctuated_data = {}
        for key, values in base_data.items():
            # 生成波动值并保留两位小数
            fluctuated_data[key] = [round(value + random.uniform(-fluctuation_range, fluctuation_range), 2) for value in values]

        # 发送 POST 请求
        response = requests.post(url, json=fluctuated_data)

        # 检查响应状态码
        if response.status_code == 200:
            print(f"数据发送成功: {fluctuated_data}")
        else:
            print(f"数据发送失败，状态码: {response.status_code}, 响应内容: {response.text}")

        # 等待 1 秒钟
        time.sleep(1)

except KeyboardInterrupt:
    print("程序被手动终止。")
except Exception as e:
    print(f"发生错误: {e}")